package com.hctforgreen.greenservice.ui.adapter;

import java.util.List;

import android.app.Activity;
import android.view.View;
import android.view.ViewGroup;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.BaseAdapter;
import android.widget.LinearLayout;
import android.widget.ListView;
import android.widget.TextView;

import com.hctforgreen.greenservice.R;
import com.hctforgreen.greenservice.model.SerieListEntity.ChildSerieEntity;
import com.hctforgreen.greenservice.model.SerieListEntity.SerieEntity;
import com.hctforgreen.greenservice.ui.widget.ChildSeriesListPopupView;

public class ChildSeriesLstAdapter extends BaseAdapter implements
		OnItemClickListener {
	private List<ChildSerieEntity> mEntities;
	private ListView mSecondLv;
	private Activity mActivity;
	private ChildSerieEntity mCurrentSecondEntity;

	private SerieEntity mCurrentFirstEntity;
	
	private ChildSeriesListPopupView mListPopupView;
	public ChildSeriesLstAdapter(ListView secondLv, Activity activity,
			SerieEntity currentFirstEntity,
			ChildSerieEntity currentSecondEntity, ChildSeriesListPopupView listPopupView) {
		mSecondLv = secondLv;
		mActivity = activity;
		
		mCurrentFirstEntity = currentFirstEntity;
		mEntities = currentFirstEntity.childList;
		mCurrentSecondEntity = currentSecondEntity;
		
		mListPopupView = listPopupView;
		mSecondLv.setAdapter(this);
		mSecondLv.setOnItemClickListener(this);
		
		initSelection(currentSecondEntity);
	}
	private void initSelection(ChildSerieEntity currentSecondEntity) {
		if (currentSecondEntity == null) {
			return;
		}
		for (int i = 0; i < mEntities.size(); i++) {
			ChildSerieEntity entity = mEntities.get(i);
			if (entity.childSeriesId.equals(currentSecondEntity.childSeriesId)) {
				mSecondLv.setSelection(i);
				entity.isSelected = true;
			}
		}
	}
	
	@Override
	public int getCount() {
		return mEntities.size();
	}

	@Override
	public Object getItem(int position) {
		return mEntities.get(position);
	}

	@Override
	public long getItemId(int position) {
		return position;
	}

	@Override
	public View getView(int position, View convertView, ViewGroup parent) {
		ViewCache viewCache;
		if (convertView == null) {
			convertView = View.inflate(mActivity,
					R.layout.item_series_books_second_lst, null);
			viewCache = new ViewCache(convertView);
			convertView.setTag(viewCache);

		} else {
			viewCache = (ViewCache) convertView.getTag();
		}

		final ChildSerieEntity entity = (ChildSerieEntity) getItem(position);

		if (entity.isSelected) {
			viewCache.getSelectorLyt().setBackgroundResource(
					R.drawable.ic_first_popup_item_selected_bg);
		} else {
			viewCache.getSelectorLyt().setBackgroundDrawable(null);
		}
		viewCache.getItemNameTv().setText(entity.childSeriesName);
		return convertView;
	}

	class ViewCache {
		private View baseView;

		private LinearLayout selectorLyt;
		private TextView itemNameTv;

		public ViewCache(View view) {
			baseView = view;
		}

		public LinearLayout getSelectorLyt() {
			if (selectorLyt == null) {
				selectorLyt = (LinearLayout) baseView
						.findViewById(R.id.lyt_selector);
			}
			return selectorLyt;
		}

		public TextView getItemNameTv() {
			if (itemNameTv == null) {
				itemNameTv = (TextView) baseView
						.findViewById(R.id.tv_item_name);
			}
			return itemNameTv;
		}
	}

	@Override
	public void onItemClick(AdapterView<?> parent, View view, int position,
			long id) {
		final ChildSerieEntity currentEntity = (ChildSerieEntity) getItem(position);

//		if(currentEntity.dataList == null){
//			return;
//		}
		clearmEntitiesSelected(currentEntity);
		currentEntity.isSelected = true;
		/**
		 * 通知getView()刷新
		 */
		notifyDataSetChanged();
		
		mListPopupView.updateWindow(currentEntity);
		mListPopupView.dismiss();
		
	}
	private void clearmEntitiesSelected(final ChildSerieEntity currentEntity) {
		for (ChildSerieEntity entity : mEntities) {
			if (entity.childSeriesId != currentEntity.childSeriesId && entity.isSelected) {
				entity.isSelected = false;
			}
		}
	}
}
